<?php

/**
 * 
 * 删除省级信息
 * 
 */

include('./config/db.php');
$pgdb = new PGDB;

if(isset($_POST) && !empty($_POST['id'])){
    // 1. 获取省的ID
    $id = $_POST['id'];

    // 2.获取到省下所有的市ID
    $get_city_sql = "select * from public.management_t where par_id = $id";
    $city_res =@$pgdb->query($get_city_sql);
    while($city_row = (pg_fetch_row($city_res))){
        // 3. 根据市的ID获取所有的区ID
        $area_sql = "select * from public.management_t where par_id = $city_row[0]";
        $area_res =@$pgdb->query($area_sql);
        // 4. 开始删除区
        while($area_row = (pg_fetch_row($area_res))){
            $area_del_sql = "delete from public.management_t where id = $area_row[0]";
            $res = @$pgdb->query($area_del_sql);
            if(!$res){
                die(json_encode(array('Code'=>0x000002,'Msg'=>'删除区失败')));
            }
        }
        // 5. 删除市
        $city_del_sql = "delete from public.management_t where id = $city_row[0]";
        $res = @$pgdb->query($city_del_sql);
        if(!$res){
            die(json_encode(array('Code'=>0x000002,'Msg'=>'删除市失败')));
        }
    }
    // 6. 删除省
    $del_provinces_sql = "delete from public.management_t where id = $id";
    $res = @$pgdb->query($del_provinces_sql);
    if(!$res){
        die(json_encode(array('Code'=>0x000002,'Msg'=>'删除省失败')));
    }
    die(json_encode(array('Code'=>0x000000,'Msg'=>'删除省成功')));
}else{
    die(json_encode(array('Code'=>0x000001,'Msg'=>'没有数据上传')));
}